package com.ugrow.ugrowcommunity.activity.warranty;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

class MyOpenHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "noteSQLite.db";
    private static final String TABLE_NAME = "note";

    private static final String CREATE_TABLE_SQL = "create table " + TABLE_NAME + "(id integer primary key autoincrement,title text,content text,create_time text,success integer)";


    public MyOpenHelper(@Nullable Context context) {
        super(context, DB_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long insertData(NoteBean noteBean) {
        SQLiteDatabase db = getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("title", noteBean.getTitle());
        values.put("content", noteBean.getContent());
        values.put("create_time", noteBean.getCreatedTime());
        values.put("success", noteBean.isSuccessful() ? 1 : 0);

        return db.insert(TABLE_NAME, null, values);

    }

    public List<NoteBean> queryAll() {
        SQLiteDatabase db = getWritableDatabase();
        List<NoteBean> noteBeanList = new ArrayList<>();

        Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex("id"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                String create_time = cursor.getString(cursor.getColumnIndex("create_time"));
                int success = cursor.getInt(cursor.getColumnIndex("success"));
                NoteBean noteBean = new NoteBean();
                noteBean.setId(id);
                noteBean.setTitle(title);
                noteBean.setContent(content);
                noteBean.setCreatedTime(create_time);
                noteBean.setSuccessful(success == 1);

                noteBeanList.add(noteBean);
            }
            cursor.close();
        }
        return noteBeanList;
    }

    public List<NoteBean> queryByTitle(String title) {
        if (TextUtils.isEmpty(title)) {
            return queryAll();
        }
        SQLiteDatabase db = getWritableDatabase();
        List<NoteBean> noteBeanList = new ArrayList<>();
        //模糊查询用%查询条件
        Cursor cursor = db.query(TABLE_NAME, null, "title like ?", new String[]{"%" + title + "%"}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex("id"));
                String title1 = cursor.getString(cursor.getColumnIndex("title"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                String create_time = cursor.getString(cursor.getColumnIndex("create_time"));
                int success = cursor.getInt(cursor.getColumnIndex("success"));

                NoteBean noteBean = new NoteBean();
                noteBean.setId(id);
                noteBean.setTitle(title1);
                noteBean.setContent(content);
                noteBean.setCreatedTime(create_time);
                noteBean.setSuccessful(success == 1);

                noteBeanList.add(noteBean);
            }
            cursor.close();
        }
        return noteBeanList;

    }

    public int updateData(NoteBean noteBean) {
        SQLiteDatabase db = getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("title", noteBean.getTitle());
        values.put("content", noteBean.getContent());
        values.put("create_time", noteBean.getCreatedTime());
        values.put("success", noteBean.isSuccessful() ? 1 : 0);

        return db.update(TABLE_NAME, values, "id like ?", new String[]{noteBean.getId()});
    }

    public int deleteDataById(String id) {
        SQLiteDatabase db = getWritableDatabase();

        return db.delete(TABLE_NAME, "id like ?", new String[]{id});

    }
}
